home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj008.zip / ORG.ZIP / TOOLBAR.PAS < prev   
Pascal/Delphi Source File  |  1992-06-26  |  9KB  |  279 lines

  1. {******************************************************************}
  2. {                                                                  }
  3. {                          Toolbar unit                            }
  4. {                  (c) 1991 Applied Visions, Inc.                  }
  5. {                                                                  }
  6. {******************************************************************}
  7.  
  8. unit ToolBar;
  9.  
  10. interface
  11.  
  12. uses WObjects, WinTypes, WinProcs,
  13.      OGL1, OGL2, OGL3;
  14.  
  15. const
  16.     ToolSize = 11;
  17. type
  18. IDArray = array [0..ToolSize] of integer;
  19. BitmapArray = array [0..ToolSize] of Pchar;
  20.  
  21. PToolBar = ^TToolBar;
  22. TToolBar = object(TGWindow)
  23.   ToolIDs : IDArray;
  24.   ToolBitmaps : BitmapArray;
  25.   SelectedTool: PGBitmap;
  26.   constructor Init(AParent: PGWindow);
  27.   destructor  Done;                                      virtual;
  28.   procedure setIDs(IDs : IDArray);
  29.   procedure setBitmaps(Bitmaps : BitmapArray);
  30.   procedure SetupWindow;                                 virtual;
  31.   procedure GPaint(APort: PPort; BadRect: PMathRect);    virtual;
  32.   procedure MarkSelection(Selection: PGBitmap; APort: PPort); virtual;
  33.   procedure BeginDrag(APoint: PGPoint; KeyStates: Word); virtual;
  34.   procedure Drag(APoint: PGPoint; KeyStates: Word);      virtual;
  35.   procedure EndDrag(APoint: PGPoint; KeyStates: Word);   virtual;
  36.   function  SelectTool(ATool: PGBitmap): Boolean;        virtual;
  37.   procedure SelectID(ID: Integer);                       virtual;
  38. end;
  39.  
  40. implementation
  41.  
  42. {const
  43.   ToolSize = 10;
  44.   IDs : array [0..ToolSize] of Integer = (710, 732, 711, 719, 713,
  45.                     714, 715, 716, 718, 721, 24340);
  46.  
  47.   Maps: array [0..ToolSize] of PChar   = ('open', 'save', 'find',
  48.                     'note', 'dial', 'help', 'new',
  49.                     'modify', 'delete', 'setup', 'exit');
  50.  }
  51.  
  52. {------------------------------------------------------------------
  53.  Creates an instance of the ToolBar, binding it to the given Parent
  54.  window.
  55.  ------------------------------------------------------------------
  56. }
  57.  
  58. constructor TToolBar.Init(AParent: PGWindow);
  59. begin
  60.   TGWindow.Init(AParent, 'ToolBar');
  61.   Attr.Style := Attr.Style or ws_Child or ws_Border;
  62.  
  63.   SelectedTool := nil;
  64. end;
  65.  
  66. procedure TToolBar.setIDs(IDs : IDArray);
  67. begin
  68.   ToolIDs := IDs;
  69. end;
  70.  
  71. procedure TToolBar.setBitmaps(Bitmaps : BitmapArray);
  72. begin
  73.   ToolBitmaps := Bitmaps;
  74. end;
  75.  
  76.  
  77. {------------------------------------------------------------------
  78.  Completes the construction of the ToolBar by loading the bitmaps,
  79.  identified in the Maps list, into the Picture instance variable.
  80.  As each bitmap is loaded, it is assigned its Menu Command ID as its
  81.  Graphic Id data field.  This is used when the bitmap is selected,
  82.  to send a command to the parent window.  
  83.  ------------------------------------------------------------------
  84. }
  85. procedure TToolBar.SetupWindow;
  86. var
  87.   ABitmap: PGBitmap;
  88.   I      : Integer;
  89.   APnt   : TGPoint;
  90.   Black  : TColor;
  91.   Gray   : TColor;
  92.   ABrush : TBrush;
  93. begin
  94.   Gray.InitDefault;
  95.   ABrush.InitDefault;
  96.   Gray.SetRGB(192, 192, 192);
  97.   ABrush.SetColor(@Gray);
  98.  
  99.   SetGCursor(cs_Finger);  { Selects default cursor for the window }
  100.   SetBrush(@ABrush);       { Sets the window's background brush }
  101.  
  102.   APnt.Init(0,0);
  103.   for I := 0 to ToolSize do
  104.   begin
  105.     New(ABitmap, Init(1,0, 0,0, tc_Tools));
  106.     ABitmap^.LoadBMPResource(ToolBitmaps[I], Space);
  107.     ABitmap^.Id := ToolIDs[I];
  108.     if Picture^.Count > 0 then
  109.     begin
  110.       APnt.Y := Picture^.Last^.Bottom;
  111.       ABitmap^.Offset(@APnt);
  112.     end;
  113.     Picture^.Add(ABitmap);
  114.   end;
  115.  
  116.   APnt.Done;
  117.   ABrush.Done;
  118.   Gray.Done;
  119. end;
  120.  
  121. {------------------------------------------------------------------
  122.  Dispose of the ToolBar.  Since all it owns are its bitmaps, the
  123.  disposal of Picture in TGWindow.Done will take care of that.
  124.  NOTE that the SelectedTool IVar only points into Picture, it does
  125.  not represent a separate object, and need not be disposed.
  126.  ------------------------------------------------------------------
  127. }
  128. destructor TToolBar.Done;
  129. begin
  130.   TGWindow.Done;
  131. end;
  132.  
  133. {------------------------------------------------------------------
  134.  Draw the ToolBar when invalidated.  We override the ancestral
  135.  GPaint for two reasons: we can use FastDraw since we don't care
  136.  about the pen or brush, and we want to invert the selected
  137.  tool to indicate its selection.
  138.  ------------------------------------------------------------------
  139. }
  140. procedure TToolBar.GPaint(APort: PPort; BadRect: PMathRect);
  141. begin
  142.   Picture^.FastDraw(APort);
  143.   if SelectedTool <> nil then
  144.     MarkSelection(SelectedTool, APort);
  145. end;
  146.  
  147. {------------------------------------------------------------------
  148.  Highlights the given bitmap to mark it as a selection.  This is
  149.  done through a slightly complex double-inversion aimed at inverting
  150.  only the border of the button to make it look pushed-in.
  151.  ------------------------------------------------------------------
  152. }
  153. procedure TToolBar.MarkSelection(Selection: PGBitmap; APort: PPort);
  154. var
  155.   BRect: TRectangle;
  156. begin
  157.   if Selection <> nil then
  158.   begin
  159.     BRect.InitDefault;
  160.     Selection^.GetBoundsRect(BRect);
  161.     BRect.Inflate(-1,-1);
  162.     BRect.Invert(APort);
  163.     BRect.Inflate(-1,-1);
  164.     BRect.Invert(APort);
  165.     BRect.Done;
  166.   end;
  167. end;
  168.  
  169. {------------------------------------------------------------------
  170.  If a new tool is selected, re-invert the previous selection and
  171.  invert the new one.
  172.  ------------------------------------------------------------------
  173. }
  174. function TToolBar.SelectTool(ATool: PGBitmap): Boolean;
  175. begin
  176.   if ATool <> nil then
  177.     if ATool <> SelectedTool then
  178.     begin
  179.       Port^.Associate(@Self);
  180.       if SelectedTool <> nil then
  181.         MarkSelection(SelectedTool, Port);
  182.  
  183.       SelectedTool := ATool;
  184.       MarkSelection(SelectedTool, Port);
  185.       Port^.Dissociate;
  186.       SelectTool := True;
  187.     end
  188.     else
  189.       SelectTool := False
  190.   else
  191.     SelectTool := False;
  192. end;
  193.  
  194. {------------------------------------------------------------------
  195.  Used to synchronize the Palette with selections made through
  196.  the menu.
  197.  ------------------------------------------------------------------
  198. }
  199. procedure TToolBar.SelectID(ID: Integer);
  200. var
  201.   Index: Integer;
  202.   I    : Integer;
  203. begin
  204.   Index := -1;
  205.   for I := 0 to ToolSize do
  206.     if ToolIDs[I] = ID then
  207.       Index := I;
  208.   if Index >= 0 then
  209.     SelectTool(PGBitmap(Picture^.At(Index)));
  210. end;
  211.  
  212. {------------------------------------------------------------------
  213.  If the mouse has been clicked over a tool's Graphic, select that
  214.  Graphic and highlight it.  The message for that button will only
  215.  be set if it is still selected in EndDrag.
  216.  ------------------------------------------------------------------
  217. }
  218. procedure TToolBar.BeginDrag(APoint: PGPoint; KeyStates: Word);
  219. var
  220.   ATool : PGraphic;
  221.   Index : Integer;
  222. begin
  223.   ATool := Picture^.ThatContains(APoint);
  224.   Index := -1;
  225.  
  226.   if ATool <> nil then
  227.     if SelectTool(PGBitmap(ATool)) then
  228.       Index := Picture^.IndexOf(SelectedTool);
  229. end;
  230.  
  231. {------------------------------------------------------------------
  232.  If the mouse has been released over a tool's Graphic, and that
  233.  button is currently selected, then clear the highlight and send
  234.  its associated command to its parent.  Recall that the Command ID
  235.  is stored in the Bitmap object as its Id instance variable.
  236.  
  237.  Note that, since this toolbar models "momentary" buttons, we clear
  238.  the highlight of the button when it is released.
  239.  ------------------------------------------------------------------
  240. }
  241. procedure TToolBar.EndDrag(APoint: PGPoint; KeyStates: Word);
  242. var
  243.   ATool : PGBitmap;
  244.   Index : Integer;
  245. begin
  246.   ATool := PGBitmap(Picture^.ThatContains(APoint));
  247.   Index := -1;
  248.  
  249.   if (ATool <> nil) and (ATool = SelectedTool) then
  250.   begin
  251.     Port^.Associate(@Self);
  252.     MarkSelection(SelectedTool, Port);  { "Release" the button }
  253.     Port^.Dissociate;
  254.     SendMessage(Parent^.HWindow, wm_Command, SelectedTool^.Id, 0);
  255.     SelectedTool := nil;
  256.   end;
  257. end;
  258.  
  259. {------------------------------------------------------------------
  260.  If the mouse is moved outside the current selection, then clear
  261.  the highlight of that selection.
  262.  ------------------------------------------------------------------
  263. }
  264. procedure TToolBar.Drag(APoint: PGPoint; KeyStates: Word);
  265. var
  266.   ATool: PGBitmap;
  267. begin
  268.   ATool := PGBitmap(Picture^.ThatContains(APoint));
  269.  
  270.   if (SelectedTool <> nil) and (ATool <> SelectedTool) then
  271.   begin
  272.     Port^.Associate(@Self);
  273.     MarkSelection(SelectedTool, Port);
  274.     Port^.Dissociate;
  275.     SelectedTool := nil;
  276.   end;
  277. end;
  278.  
  279. end.